#!/bin/bash

# set the name of v8 shell
shell="./shell_g"

# if the '-g' debug flag then use debug shell
#while getopts ":g" opt; do
#  case $opt in
#    g)
#      #echo "-g was triggered!" >&2
#      shell=$shell"_g"
#      shift
#      ;;
#    \?)
#      echo "Invalid option: -$OPTARG" >&2
#      ;;
#  esac
#done

# a file arg is required
if [[ -z $1 ]] ; then
    echo "Usage: ast.sh JSFILE"
    exit
fi
file=$1

# test shell exists
if [[ ! -f $shell ]] ; then
    # try parent dir
    shell=".$shell"
    if [[ ! -f $shell ]] ; then
        echo "Error: v8 $shell not found"
        exit
    fi
fi

# test javascript file exists
if [[ ! -f $file ]] ; then
    echo "Error: file $file not found"
    exit
fi

# test example.js file exists
if [[ ! -f "example.js" ]] ; then
    echo "Error: file example.js not found"
    exit
fi

# sed will choak on forward slashes in filename so escape them - using sed!
file=$(echo $file | sed 's/\//\\\//g')
#echo $file
example=$(sed -e "s/var source.*/var source=read\(\"$file\"\);/g" example.js)
#echo $example
$shell -e "$example"























